package com.lineage.server.model.Instance;

import com.lineage.server.datatables.DoorSpawnTable;
import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.L1War;
import com.lineage.server.model.L1WarSpawn;
import com.lineage.server.serverpackets.S_CastleMaster;
import com.lineage.server.serverpackets.S_PacketBoxWar;
import com.lineage.server.serverpackets.S_RemoveObject;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldWar;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1CrownInstance extends L1NpcInstance {
  private static final long serialVersionUID = 1L;
  
  private static final Log _log = LogFactory.getLog(L1CrownInstance.class);
  
  public L1CrownInstance(L1Npc template) {
    super(template);
  }
  
  public void onAction(L1PcInstance player) {
    try {
      boolean in_war = false;
      if (player.getClanid() == 0)
        return; 
      String playerClanName = player.getClanname();
      L1Clan clan = WorldClan.get().getClan(playerClanName);
      if (clan == null)
        return; 
      if (player.isDead())
        return; 
      if (player.getCurrentHp() <= 0)
        return; 
      if (player.isGhost())
        return; 
      if (player.isTeleport())
        return; 
      if (!player.isCrown())
        return; 
      if (player.getTempCharGfx() != 0 && player.getTempCharGfx() != 1)
        return; 
      if (player.getWeapon() != null) {
        player.sendPackets((ServerBasePacket)new S_ServerMessage("请解除武器才能夺城"));
        return;
      } 
      if (player.getId() != clan.getLeaderId())
        return; 
      if (!checkRange(player))
        return; 
      if (clan.getCastleId() != 0) {
        player.sendPackets((ServerBasePacket)new S_ServerMessage(474));
        return;
      } 
      int castle_id = L1CastleLocation.getCastleId(getX(), getY(), getMapId());
      boolean existDefenseClan = false;
      L1Clan defence_clan = (L1Clan)L1CastleLocation.mapCastle().get(new Integer(castle_id));
      if (defence_clan != null)
        existDefenseClan = true; 
      List<L1War> wars = WorldWar.get().getWarList();
      Iterator<L1War> iterator = wars.iterator();
      while (iterator.hasNext()) {
        L1War war = iterator.next();
        if (castle_id == war.getCastleId()) {
          in_war = war.checkClanInWar(playerClanName);
          break;
        } 
      } 
      if (existDefenseClan && !in_war)
        return; 
      Iterator<L1War> iterator2 = wars.iterator();
      while (iterator2.hasNext()) {
        L1War war = iterator2.next();
        if (war.checkClanInWar(playerClanName) && existDefenseClan) {
          war.winCastleWar(playerClanName);
          break;
        } 
      } 
      if (existDefenseClan && defence_clan != null) {
        defence_clan.setCastleId(0);
        ClanReading.get().updateClan(defence_clan);
        if (L1CastleLocation.mapCastle().get(new Integer(castle_id)) != null)
          L1CastleLocation.removeCastle(new Integer(castle_id)); 
        World.get().broadcastPacketToAll((ServerBasePacket)new S_CastleMaster(0, defence_clan.getLeaderId()));
      } 
      clan.setCastleId(castle_id);
      ClanReading.get().updateClan(clan);
      if (L1CastleLocation.mapCastle().get(new Integer(castle_id)) == null)
        L1CastleLocation.putCastle(Integer.valueOf(castle_id), clan); 
      if (castle_id == 2) {
        World.get().broadcastPacketToAll((ServerBasePacket)new S_CastleMaster(8, player.getId()));
      } else {
        World.get().broadcastPacketToAll((ServerBasePacket)new S_CastleMaster(castle_id, player.getId()));
      } 
      int[] loc = new int[3];
      for (L1PcInstance pc : World.get().getAllPlayers()) {
        if (pc.getClanid() != player.getClanid() && !pc.isGm() && 
          L1CastleLocation.checkInWarArea(castle_id, pc)) {
          loc = L1CastleLocation.getGetBackLoc(castle_id);
          int locx = loc[0];
          int locy = loc[1];
          short mapid = (short)loc[2];
          L1Teleport.teleport(pc, locx, locy, mapid, 5, true);
        } 
      } 
      L1PcInstance[] clanMember = clan.getOnlineClanMember();
      if (clanMember.length > 0) {
        L1PcInstance[] array;
        int k = (array = clanMember).length;
        int j = 0;
        while (j < k) {
          L1PcInstance pc2 = array[j];
          if (pc2.getId() == clan.getLeaderId())
            pc2.sendPackets((ServerBasePacket)new S_PacketBoxWar(3)); 
          pc2.sendPackets((ServerBasePacket)new S_PacketBoxWar(4));
          j++;
        } 
      } 
      deleteMe();
      Iterator<L1Object> iterator4 = World.get().getObject().iterator();
      while (iterator4.hasNext()) {
        L1Object l1object = iterator4.next();
        if (l1object instanceof L1TowerInstance) {
          L1TowerInstance tower = (L1TowerInstance)l1object;
          if (L1CastleLocation.checkInWarArea(castle_id, tower))
            tower.deleteMe(); 
        } 
        if (l1object instanceof L1CatapultInstance) {
          L1CatapultInstance catapult = (L1CatapultInstance)l1object;
          if (!L1CastleLocation.checkInWarArea(castle_id, catapult))
            continue; 
          catapult.deleteMe();
        } 
      } 
      L1WarSpawn warspawn = new L1WarSpawn();
      warspawn.spawnTower(castle_id);
      warspawn.SpawnCatapult(castle_id);
      L1DoorInstance[] doorList;
      int length = (doorList = DoorSpawnTable.get().getDoorList()).length;
      int i = 0;
      while (i < length) {
        L1DoorInstance door = doorList[i];
        if (L1CastleLocation.checkInWarArea(castle_id, door))
          door.repairGate(); 
        i++;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void deleteMe() {
    this._destroyed = true;
    if (getInventory() != null)
      getInventory().clearItems(); 
    allTargetClear();
    this._master = null;
    World.get().removeVisibleObject((L1Object)this);
    World.get().removeObject((L1Object)this);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      pc.removeKnownObject((L1Object)this);
      pc.sendPackets((ServerBasePacket)new S_RemoveObject((L1Object)this));
    } 
    removeAllKnownObjects();
  }
  
  private boolean checkRange(L1PcInstance pc) {
    return (getX() - 1 <= pc.getX() && pc.getX() <= getX() + 1 && getY() - 1 <= pc.getY() && 
      pc.getY() <= getY() + 1);
  }
}
